import threading

from module.data.TickerSimulater import PushTickerSimulater
from module.data.PreTickerHandler import preTickerHandler

from module.utils import TaskAdminUtil

import logging
log = logging.getLogger("tasklog")

class SimulatThread(threading.Thread):
    def __init__(self, taskid, taskDetail):
        threading.Thread.__init__(self)
        self.taskid = taskid
        self.taskDetail = taskDetail

    def run(self):
        try:
            code = self.taskDetail[0]
            datadate = self.taskDetail[1]
            starttime = self.taskDetail[2]
            endtime = self.taskDetail[3]

            tickerData = preTickerHandler(code, datadate, starttime, endtime)
            if tickerData:
                log.info("%s任务持续运行: 详情信息-%s 加载数据-%s" % (self.taskid, self.taskDetail, len(tickerData)))
                taskCenter = TaskAdminUtil.pullTaskCenter(self.taskid)
                simulater = PushTickerSimulater(taskCenter[0]['uuid'], taskCenter[0]['taskid'])
                simulater.ticker_data_push(self.taskDetail, tickerData)
            else:
                log.info("%s任务持续运行: 任务信息-%s 无数据" % (self.taskid, self.taskDetail))

        except Exception as err:
            log.error("%s任务持续运行异常:%s 任务信息：%s" % (self.taskid, err, self.taskDetail))


